package map;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: <a href="https://leetcode.cn/problems/longest-substring-without-repeating-characters/">3. 无重复字符的最长子串</a>
 * @author: zhaoyinglin
 * @date: 2024/3/6
 */
public class LengthOfLongestSubstring {

    public static void main(String[] args) {

    }

    public int lengthOfLongestSubstring(String s) {

        Map<Character, Integer> charMap = new HashMap<>();

        int left = 0, maxLen = 0;

        char[] chars = s.toCharArray();

        for (int i = 0; i < chars.length; i++) {
            char ch = chars[i];
            if (charMap.containsKey(ch)) {
                left = charMap.get(ch) + 1;
            }
            charMap.put(ch, i);
            maxLen = Math.max(maxLen, i - left + 1);
        }

        return maxLen;
    }
}
